package com.acxy.weixin.service;

import com.acxy.ants.common.kit.HttpKit;
import com.acxy.ants.core.annotation.Service;
import com.acxy.ants.core.annotation.Value;
import com.acxy.weixin.kit.ApiResult;
import com.acxy.weixin.kit.AccessToken;

import java.util.HashMap;
import java.util.Map;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-10-27
 */
@Service
public class WxMenuService {

    @Value("wx.appId")
    private String appId;

    @Value("wx.appSecret")
    private String appSecret;

    //创建自定义菜单 POST
    private final static String menu_create_api = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

    //删除自定义菜单 GET
    private final static String menu_delete_api = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";

    //查询自定义菜单 GET
    private final static String menu_get_api = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";

    //添加个性菜单 POST
    private final static String conditional_add_api = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=ACCESS_TOKEN";

    //删除个性菜单 POST
    private final static String conditional_del_api = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=ACCESS_TOKEN";

    //测试个性化菜单匹配结果 POST
    private final static String try_match_api = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=ACCESS_TOKEN";

    //自定义菜单或者微信运营者设置菜单
    private final static String current_self_menu_api = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN";


    //查询所有自定义菜单
    public ApiResult getMenu(){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendGET(menu_get_api.replace("ACCESS_TOKEN", accessToken));
        return new ApiResult(result);
    }


    //自定义菜单删除接口可以删除所有自定义菜单（包括默认菜单和全部个性化菜单）
    public ApiResult deleteMenu(){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendGET(menu_delete_api.replace("ACCESS_TOKEN", accessToken));
        return new ApiResult(result);
    }

    //创建自定义菜单
    public ApiResult createMenu(String jsonStr){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendPost(menu_create_api.replace("ACCESS_TOKEN", accessToken), jsonStr);
        return new ApiResult(result);
    }

    //添加个性化菜单
    public ApiResult addConditional(String jsonStr) {
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendPost(conditional_add_api.replace("ACCESS_TOKEN", accessToken), jsonStr);
        return new ApiResult(result);
    }


    /**
     * 删除个性化菜单
     * @param menuId 菜单ID
     * @return
     */
    public ApiResult delConditional(String menuId) {
        return null;
    }



    /**
     * 测试个性化菜单匹配结果
     * @param userId user_id可以是粉丝的OpenID，也可以是粉丝的微信号。
     * @return
     */
    public ApiResult tryMatch(String userId) {
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        Map map = new HashMap<>();
        map.put("user_id", userId);
        String result = HttpKit.sendPost(try_match_api.replace("ACCESS_TOKEN", accessToken), map);
        return new ApiResult(result);
    }

    /**
     * 获取自定义菜单或者微信运营者设置菜单
     * @return
     */
    public ApiResult getCurrentSelfMenu() {
        return null;
    }
}
